package cc.eddic.pratice.programmingparadigms.streampractice.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.SubCategory;
import cc.eddic.pratice.programmingparadigms.exam.impl.*;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAccuracy;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAndAnswer;
import cc.eddic.pratice.programmingparadigms.streampractice.ScoreOfStudentAndCategory;
import cc.eddic.pratice.programmingparadigms.streampractice.StreamPractice;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Slf4j
public class StreamPracticeHrjImpl extends QuestionBankServiceZyzImpl implements StreamPractice {
    @Override
    public List<QuestionAndAnswer> mapToQuestionAndAnswer(List<Question> questionList) {
        List<QuestionAndAnswer> questionAndAnswersList = new ArrayList<>();

        QuestionBankServiceHrjImpl questionBankService = new QuestionBankServiceHrjImpl();

        List<Question> questions = questionBankService.importCorrectAnswers("");

        for (Question q : questions) {
            for (Option option : q.getOptions()) {
                if (option.isCorrectAnswer()) {
                    questionAndAnswersList.add(new QuestionAndAnswer(q.getNo(), q.getDescription(), option.getChoice(), option.getAnswer()));
                }
            }
        }

        return questionAndAnswersList;
    }


    public List<QuestionAccuracy> calculateAccuracy(List<Answer> answerList) {
        QuestionBankServiceHrjImpl questionBankService = new QuestionBankServiceHrjImpl();
        ExamServiceHrjImpl examServiceHrjImpl = new ExamServiceHrjImpl();
        Map<String, String> mapOfStudent = new HashMap<>();
        Map<Integer, Integer> correctNumOfQuestion = new HashMap<>();
        List<Answer> answers = examServiceHrjImpl.importAnswers("");
        List<AnswerModel> correctAnswer = questionBankService.readCorrectAnswers("");
        List<QuestionAccuracy> accuracyResult = new ArrayList<>();

        //考生人数
        for (Answer a : answers) {
            mapOfStudent.put(a.getStudent().getName(), "a");
        }

        //每道题答对人数
        for (AnswerModel am : correctAnswer) {
            int correctNum = 0;
            for (Answer a : answers) {
                if (am.getQuestionNo() == a.getQuestion().getNo()) {
                    if (am.getChoice() == a.getChoice()) {
                        correctNum = correctNum + 1;
                    }
                }
            }
            correctNumOfQuestion.put(am.getQuestionNo(), correctNum);
        }

        //计算每题正确率
        List<Question> questions = questionBankService.importCorrectAnswers("");
        for (Question q : questions) {
            double accuracy = (double) correctNumOfQuestion.get(q.getNo()) / mapOfStudent.keySet().size();
            System.out.println(accuracy);
            accuracyResult.add(new QuestionAccuracy(q.getNo(), q.getDescription(), accuracy));
        }

        return accuracyResult;
    }

    @Override
    public List<ScoreOfStudentAndCategory> calculateScoreOfStudentAndCategory(List<Answer> answerList) {


        return StreamPractice.super.calculateScoreOfStudentAndCategory(answerList);
    }

    @Override
    public List<ScoreOfStudentAndCategory> calculateScoreOfStudentAndSubCategory(List<Answer> answerList) {
        QuestionBankServiceHrjImpl questionBankServiceHrj = new QuestionBankServiceHrjImpl();
        ExamServiceHrjImpl examServiceHrj = new ExamServiceHrjImpl();

        List<Question> questions = questionBankServiceHrj.importCategories("C:\\Users\\Lenovo\\Desktop\\试题分类和考试结果.xlsx");
        List<Answer> studentsAnswer = examServiceHrj.importAnswers("");
        List<InnerModel> innerModelList = new ArrayList<>();
        val defaultOption = new Option("", '*', true);

        for (Answer ans : studentsAnswer) {
            for (Question q : questions) {

                if (ans.getQuestion().getNo() == q.getNo()) {
                    innerModelList.add(
                            new InnerModel(ans.getStudent().getName(), q.getNo(),
                                    q.getSubCategory(), ans.getChoice(), q.getOptions().stream()
                                    .filter(Option::isCorrectAnswer)
                                    .findFirst()
                                    .orElse(defaultOption)
                                    .getChoice()));
                }

            }

        }


        List<InnerModel> correctAnswer = innerModelList.stream().filter(innerModel -> innerModel.choice == innerModel.correctAnswer).collect(toList());

        Map<String, List<InnerModel>> groupByName = correctAnswer.stream().collect(Collectors.groupingBy(innerModel -> innerModel.studentName));

        Map<String, Map<String, Integer>> result = new HashMap<>();
        for (String name : groupByName.keySet()) {
            Map<String, Integer> scoreMap = new HashMap<>();
            System.out.println(name + "groupByName.get(name)" + groupByName.get(name).size());
            int score =0;
            for (int i = 0; i < groupByName.get(name).size(); i++) {
                score = score+1;

                scoreMap.put(groupByName.get(name).get(i).getQuestionSubCategory().getName(), score);
                System.out.println("00000"+scoreMap);

            }
            result.put(name, scoreMap);
        }

        return null;


    }




    @Getter
    private static class InnerModel {

        public InnerModel(String studentName, int questionNo, SubCategory questionSubCategory, char choice, char correctAnswer) {
            this.studentName = studentName;
            this.questionNo = questionNo;
            this.questionSubCategory = questionSubCategory;
            this.choice = choice;
            this.correctAnswer = correctAnswer;
        }

        public boolean isCorrect() {
            return choice == correctAnswer;
        }

        String studentName;
        int questionNo;
        SubCategory questionSubCategory;
        char choice;
        char correctAnswer;
    }
}